package com.scgk.sync.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scgk.sync.common.constant.SFTPConstants;
import com.scgk.sync.common.utils.SftpClientUtil;
import com.scgk.sync.doamin.ScgkCgRkldYdXzqdqrk;
import com.scgk.sync.mapper.ScgkCgRkldYdXzqdqrkMapper;
import com.scgk.sync.service.IScgkCgRkldYdXzqdqrkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 行政区当前人口 服务实现类
 * </p>
 *
 * @author ws
 * @since 2024-01-21
 */
@Service
@Slf4j
public class ScgkCgRkldYdXzqdqrkServiceImpl extends ServiceImpl<ScgkCgRkldYdXzqdqrkMapper, ScgkCgRkldYdXzqdqrk> implements IScgkCgRkldYdXzqdqrkService {


    @Override
    public void addXzqczrk(List<String> list) {

        List<ScgkCgRkldYdXzqdqrk> resList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(list)) {
            list.stream().forEach(item -> {
                String[] split = item.split("\\$");
                ScgkCgRkldYdXzqdqrk build = ScgkCgRkldYdXzqdqrk.builder()
                        .citycode(split[0])
                        .countycode(split[1])
                        .dqrk(Integer.valueOf(split[2]))
                        .sjd(DateUtil.format(DateUtil.parse(split[3], "yyyyMMddHH"), "yyyy-MM-dd"))
                        .hour(DateUtil.format(DateUtil.parse(split[3], "yyyyMMddHH"), "HH"))
                        .build();

                resList.add(build);
            });
        }

        this.saveBatch(resList);
    }

    /**
     * 获取 FolderName {当天时间} 目录下 的 fileName {行政区当前人口 dwa_ovm_ep_city_12res_hm_} 文件
     * @param fileName
     * @param folderName
     * @return
     * @throws Exception
     */
    @Override
    public List<String> getYdDataByFileName(String fileName, String folderName) throws Exception {
        SftpClientUtil instans = SftpClientUtil.getInstans(SFTPConstants.SFTP_REQ_HOST, SFTPConstants.SFTP_REQ_USERNAME, SFTPConstants.SFTP_REQ_PASSWORD, SFTPConstants.SFTP_REQ_PORT, "UTF-8");
        //SftpClientUtil instans = SftpClientUtil.getInstans("192.168.10.1", "test", "test", 220, "UTF-8");
        List<String> list = instans.getYdFilesDateOne(SFTPConstants.REMOTEPATH, folderName, fileName);
        return list;
    }


    @Override
    public List<String> getTest(String folderName) throws Exception {
        SftpClientUtil instans = SftpClientUtil.getInstans("192.168.10.1", "test", "test", 220, "UTF-8");
        List<String> list = instans.getTest(SFTPConstants.REMOTEPATH, folderName);
        return list;
    }
}
